package other;
import handler.accountFormHandlers.LogOutActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import customGUI.MainMenuContent;
import db.Account;
import facade.FormCreator;
import form.LogForm.LogForm;

import java.awt.BorderLayout;
import java.util.ArrayList;

public class MenuUI extends JPanel{

	JSplitPane splitPane;
	JPanel leftPanel;
	JPanel rightPanel;

	ArrayList<MainMenuContent> contentList;
	JList<String> contentStringList;
	private Account account;
	
	public MenuUI(Account account) {
		//globalusername = username;
		
		contentList = FormCreator.createMainMenuContentList(account);
		
		//init the list of content
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		for(int i=0;i<contentList.size();i++)
			listModel.addElement(contentList.get(i).getTitle());
		contentStringList = new JList<String>(listModel);
		contentStringList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//ActionListener temporarily placed here
		contentStringList.addListSelectionListener(new ListSelectionListener() {
			            public void valueChanged(ListSelectionEvent evt) {
			              if (evt.getValueIsAdjusting()){
			                return;
			              }
			              rightPanel = contentList.get(contentStringList.getSelectedIndex()).getPanel();
			              
			              if(contentStringList.getSelectedValue().equals("Logs"))
			            	  ((LogForm)rightPanel).refresh();
			              
			              splitPane.setRightComponent(rightPanel);
			            }
		            });
		
		//init the button
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new LogOutActionListener());
		
		//init the left panel
		
		JScrollPane contentTitleScrollPane = new JScrollPane(contentStringList);
		
		leftPanel = new JPanel();
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
		leftPanel.add(contentTitleScrollPane);
		leftPanel.add(btnLogOut);
		
		rightPanel = new JPanel();
		//split pane
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
		
		
		setLayout(new BorderLayout());
		add(splitPane);
		
		//setSize(new Dimension(AssetRegistryFrame.getARFrame().getWidth(), AssetRegistryFrame.getARFrame().getHeight()));
	}
	
	

	
}
